//
//  ByHomeModuleView.m
//  caipiao_ios
//
//  Created by mc on 2018/7/6.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByHomeModuleView.h"

@implementation ByHomeModuleView

- (id)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat width = SCREEN_WIDTH / 4;
    CGFloat height = width * 0.6 + 11 * 3 + 15;
    for (int i = 0; i < dataArray.count; i++) {
        
        ByNetModel *netModel = [_dataArray objectAtIndex:i];
        UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(i % 4 *width , i /  4 * height, width, height)];
        [self addSubview:control];
        control.userInteractionEnabled = YES;
        
        UIImageView *iconView = [[UIImageView alloc] init];
        [control addSubview:iconView];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(control).offset(11);
            make.centerX.equalTo(control).offset(0);
            make.size.mas_equalTo(CGSizeMake(width *0.6, width * 0.6));
        }];
        [iconView sd_setImageWithURL:[NSURL URLWithString:netModel.logo] placeholderImage:[UIImage imageNamed:@"彩种默认图"]];
        iconView.userInteractionEnabled = YES;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [control addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(iconView.mas_bottom).offset(11);
            make.left.equalTo(control).offset(2);
            make.right.equalTo(control).offset(-2);
            make.height.mas_equalTo(@15);
        }];
        titleLabel.font = FONT(15);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = BY_TEXT_COLOR;
        titleLabel.text = netModel.alias_name;
        
        control.tag = 100 + i;
        [control addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        
        iconView.tag = 200 + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewAction:)];
        [iconView addGestureRecognizer:tap];
        
        UIImageView *stateView = [[UIImageView alloc] init];
        [self addSubview:stateView];
        
        
        if (netModel.status) {
            if (netModel.status.integerValue == 1) {
                stateView.image = [UIImage imageNamed:@"待开发"];
                [stateView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(iconView.mas_top).offset(0);
                    make.right.equalTo(control).offset(0);
                    make.size.mas_equalTo(CGSizeMake(35, 18));
                }];
            }else if (netModel.status.integerValue == 2){
                stateView.image = [UIImage imageNamed:@"停售中状态"];
                [stateView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(iconView.mas_top).offset(0);
                    make.right.equalTo(control).offset(0);
                    make.size.mas_equalTo(CGSizeMake(35, 18));
                }];
            }else if (netModel.status.integerValue == 3){
                stateView.image = [UIImage imageNamed:@"今日开奖状态"];
                [stateView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(iconView.mas_top).offset(0);
                    make.right.equalTo(control).offset(0);
                    make.size.mas_equalTo(CGSizeMake(48, 18));
                }];
            }else{
                stateView.hidden = YES;
                stateView.image = [UIImage imageNamed:@"停售中状态"];
                [stateView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(iconView.mas_top).offset(0);
                    make.right.equalTo(control).offset(0);
                    make.size.mas_equalTo(CGSizeMake(35, 18));
                }];
            }
        }
        
        
    }
    
    
}
- (void)tapViewAction:(UITapGestureRecognizer *)tap{
    
    if (self.selectBlock) {
        self.selectBlock(tap.view.tag - 200);
    }
}

- (void)tapAction:(UIControl *)control{
    
    if (self.selectBlock) {
        self.selectBlock(control.tag - 100);
    }
    
}


@end
